<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>计算数组的差集</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-diff" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_diff</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_diff</span> &mdash; <span class="dc-title">计算数组的差集</span></p>

   </div> 

 <div class="refsect1 description" id="refsect1-function.array-diff-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_diff</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array1</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$array2</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
  ] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   对比 <code class="parameter">array1</code> 和其他一个或者多个数组，返回在
     <code class="parameter">array1</code> 中但是不在其他 array 里的值。
  </p>
 </div>

    <div class="refsect1 parameters" id="refsect1-function.array-diff-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array1</code></dt>

     <dd>

      <p class="para">
       要被对比的数组
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">array2</code></dt>

     <dd>

      <p class="para">
       和这个数组进行比较
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
       更多相比较的数组
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

   
    <div class="refsect1 returnvalues" id="refsect1-function.array-diff-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    返回一个数组，该数组包括了所有在
     <code class="parameter">array1</code> 中但是不在任何其它参数数组中的值。注意键名保留不变。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-diff-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="function.array-change-key-case.example-1">
    <p><strong>Example #1 <span class="function"><strong>array_diff()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>
     在 <var class="varname"><var class="varname">$array1</var></var> 中多次出现的值一样处理，输出结果为：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [1] =&gt; blue
)
</pre></div>
    </div>
   </div>
  </p>
 </div>

   
    <div class="refsect1 notes" id="refsect1-function.array-diff-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
     两个单元仅在 <em>(string) $elem1 === (string) $elem2</em>
      时被认为是相同的。也就是说，当字符串的表达是一样的时候。
      
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    注意本函数只检查了多维数组中的一维。当然可以用
      <em>array_diff($array1[0], $array2[0]);</em>
      检查更深的维度。
   </p>
  </p></blockquote>
 </div>

   
    <div class="refsect1 seealso" id="refsect1-function.array-diff-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_diff_assoc.html" class="function" rel="rdfs-seeAlso">array_diff_assoc()</a> - 带索引检查计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_intersect.html" class="function" rel="rdfs-seeAlso">array_intersect()</a> - 计算数组的交集</span></li>
    <li class="member"><span class="function"><a href="array_intersect_assoc.html" class="function" rel="rdfs-seeAlso">array_intersect_assoc()</a> - 带索引检查计算数组的交集</span></li>
   </ul>
  </p>
 </div>

   
  </div></div></div></body></html>